package cn.nawang.ebeim.api.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.nawang.ebeim.api.domain.BackStrategy;
import cn.nawang.ebeim.api.service.BackStrategyService;
import cn.nawang.ebeim.api.utils.Constant;

/**
 * @ClassName: BackStrategyController
 * @Description: TODO
 * @author fanyb
 * @date Aug 24, 2015 5:23:36 PM
 * @version v1.0
 */
@RestController
public class BackStrategyController {
  private static final Logger LOG = LoggerFactory.getLogger(BackStrategyController.class);

  @Autowired
  private BackStrategyService backStrategyService;

  /**
   * 
   * @author fanyb
   * @Description: 根据数据源编号查找备份策略对象
   * @param datasourceId 数据源编号
   * @return Map<String,Object> 备份策略对象
   * @date Aug 24, 20155:30:03 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/back_strategy/find/datasource_id", method = RequestMethod.GET)
  public Map<String, Object> findByDatasourceId(String datasourceId) {
    LOG.debug("/token/back_strategy/find/datasource_id {}", datasourceId);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    map.put("object", backStrategyService.getBackStrategy(datasourceId));
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findByDatasourceId {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新备份策略
   * @param backStrategy 备份策略对象
   * @return Map<String,Object>
   * @date Aug 27, 201510:47:30 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/back_strategy/update", method = RequestMethod.GET)
  public Map<String, Object> update(BackStrategy backStrategy) {
    LOG.debug("/token/back_strategy/update {}", backStrategy);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    boolean updateFlag = backStrategyService.updateBackStrategy(backStrategy);
    if (updateFlag) {
      map.put("code", Constant.CODE_SUCCESS);
      return map;
    }
    LOG.debug("updateBackStrategy {}", map);
    return map;
  }
}
